/**************************************************************************
 * Copyright (C) 2020-2020  Unisound
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 **************************************************************************
 *
 * Description : uni_flash.c
 * Author      : shangjinlong@unisound.com
 * Date        : 2020.02.05
 *
 **************************************************************************/

#include "uni_local_source.h"
#include "uni_hal_mailbox.h"
#include "uni_iot.h"
#include "uni_log.h"
#include "uni_mp3_resource.h"
#include "uni_audio_player.h"

#define TAG     "local_source"

typedef struct {
  uni_sem_t sem_sync_read;
  char      *source_data;
  int       read_len;
  uni_u32   source_offset;
  uni_u32   source_len;
} LocalSource;

static LocalSource g_local_source;

static void _reset_local_source_info() {
  g_local_source.source_offset = -1;
  g_local_source.source_len = -1;
}

static void _local_source_default() {
  memset(&g_local_source, 0, sizeof(LocalSource));
  _reset_local_source_info();
}

static uni_bool _is_local_source_info_init() {
  return (g_local_source.source_offset == -1 &&
          g_local_source.source_len == -1);
}

static void _sem_create() {
  uni_sem_init(&g_local_source.sem_sync_read, 0);
}

static void _sem_destroy() {
  uni_sem_destroy(g_local_source.sem_sync_read);
}

static int _read_local_source(char *args, mailbox_message *msg) {
  g_local_source.source_data = (char *)msg->data[0];
  g_local_source.read_len = msg->data[1];
  uni_sem_post(g_local_source.sem_sync_read);
  return 0;
}

static int _load_source_info(const char *filename) {
  int ret;
  if (!_is_local_source_info_init()) {
    return 0;
  }

  ret = Mp3GetResourceInfo(filename, &g_local_source.source_offset,
                           &g_local_source.source_len);
  if (ret < 0) {
    LOGW(TAG, "cannot find PCM file: %s", filename);
    return -1;
  }

  return 0;
}

static int _read_resource(int offset, int len) {
  mailbox_message msg;
  int ret = 0;
  msg.taskId = MAILBOX_FLASH;
  msg.size = 2;
  msg.data[0]= offset;
  msg.data[1]= len;

  ret = sys_hal_mailbox_send(&msg);
  if (0 != ret) {
    LOGE(TAG, "sys_hal_mailbox_send failed");
    return 0;
  }

  uni_sem_wait(g_local_source.sem_sync_read);

  invalidate_dcache_range((unsigned long)g_local_source.source_data,
                          (unsigned long)(g_local_source.source_data +
                                          g_local_source.source_len));
  return g_local_source.read_len;
}

static int _retrieve_source_data(char *buf, int len) {
  int read_size;

  if (0 == g_local_source.source_len) {
    _reset_local_source_info();
    return AUDIO_RETRIEVE_DATA_FINISHED;
  }

  read_size = uni_min(len, g_local_source.source_len);
  if (read_size <= 0) {
    return 0;
  }

  read_size = _read_resource(g_local_source.source_offset, read_size);
  memcpy(buf, g_local_source.source_data, read_size);
  g_local_source.source_len -= read_size;
  g_local_source.source_offset += read_size;
  return read_size;
}

int LocalSourceLoad(const char *fileName, char *buf, int len,
                    LocalSourceStatus status) {
  int ret;
  if (status == SOURCE_STOP) {
    _reset_local_source_info();
    return AUDIO_RETRIEVE_DATA_FINISHED;
  }

  ret = _load_source_info(fileName);
  if (ret < 0) {
    return AUDIO_RETRIEVE_DATA_FINISHED;
  }

  return _retrieve_source_data(buf, len);
}

int LocalSourceIsExist(const char *fileName) {
  int ret;
  uni_u32 offset;
  uni_u32 len;
  ret = Mp3GetResourceInfo(fileName, &offset, &len);
  if (ret < 0) {
    return 0;
  }
  return 1;
}

int LocalSourceInit() {
  _local_source_default();
  _sem_create();
  sys_hal_mailbox_register_callback(MAILBOX_PLAY, _read_local_source, NULL);
  return 0;
}

void LocalSourceFinal() {
  _sem_destroy();
}
